package com.sec.android.app.myfiles.module.trash;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.samsung.android.sdk.slinkcloud.CloudGatewayMediaStore;
import com.sec.android.app.myfiles.feature.CloudMgr;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.AbsProviderImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;

import java.util.StringTokenizer;

/**
 * Created by gw8951.kim on 2016-11-17.
 */

public class TrashFileProviderImp extends AbsProviderImp {

    public TrashFileProviderImp(Context context) {
        super(context);
    }

    @Override
    protected FileRecord.StorageType getStorage() {
        return FileRecord.StorageType.Trash;
    }

    @Override
    protected boolean needRefresh(Uri uri, FileRecord curRecord) {
        return false;
    }

    @Override
    protected void _refreshDB(Uri uri, FileRecord curRecord) {

    }

    @Override
    protected Cursor _query(Uri uri, String[] projectionIn, String selection, String[] selectionArgs, String sortOrder) {
        Uri cloudUri = CloudGatewayMediaStore.Trash.getTrashListUri(CloudMgr.getInstance(mContext).getDeviceId(FileRecord.CloudType.SamsungDrive));

        sortOrder = getSortOrder(sortOrder);
        Log.d(this, "Cloud sort after : " + sortOrder + "///");
        Cursor c = null;
        try {
            c = mContext.getContentResolver().query(cloudUri, projectionIn, selection, selectionArgs, sortOrder);
        } catch (IllegalArgumentException e) {
            Log.e(this, "IllegalArgumentException:" + e.toString());
        }
        return c;
    }

    private String getSortOrder(String sortOrder) {
        if (sortOrder != null) {
            StringTokenizer st = new StringTokenizer(sortOrder, " ");
            String tmp;
            StringBuilder sb = new StringBuilder();
            while (st.hasMoreTokens() && ((tmp = st.nextToken()) != null)) {
                if (tmp.equals(CloudGatewayMediaStore.FileBrowser.FileBrowserColumns.DISPLAY_NAME)) {
                    sb.append(tmp);
                    st.nextToken();
                    st.nextToken();
                    tmp = st.nextToken();
                    if (tmp.startsWith("DESC")) {
                        sb.append(" DESC, ");
                    } else {
                        sb.append(", ");
                    }
                } else {
                    sb.append(tmp);
                    tmp = st.nextToken();
                    if (tmp.startsWith("DESC")) {
                        sb.append(" DESC, ");
                    } else {
                        sb.append(", ");
                    }
                }
            }
            sortOrder = sb.toString();
            if ((sortOrder != null) && (sortOrder.charAt(sortOrder.length() - 2) == ',')) {
                sortOrder = sortOrder.substring(0, sortOrder.length() - 2);
            }
        }

        return sortOrder;
    }
}
